﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using RestaurateurConsole;

namespace RestaurateurConsole.Core
{
    [Serializable]
    public class DishesHandler
    {
        private List<Dish> dishes;

        public List<Dish> ListOfDishes
        {
            get
            {
                return dishes;
            }
        }

        public IEnumerable<Dish> Dishes
        {
            get
            {
                return dishes.OrderBy(D => D.DishName).AsEnumerable();
            }
        }

        public DishesHandler()
        {
            dishes = new List<Dish>();
        }

        public void Clear()
        {
            dishes.Clear();
        }

        public void Add(string name, decimal price)
        {
            int dishID = 0;
            if (dishes.Count > 0)
                dishID = dishes.Max(D => D.DishID) + 1;
            Dish newDish = new Dish() { DishID = dishID, DishName = name, DishPrice = price };
            dishes.Add(newDish);
        }

        public IEnumerable<Dish> Filter(string name)
        {
            return from d in dishes
                   where d.DishName.ToLower().IndexOf(name.ToLower()) != -1
                   orderby d.DishName ascending
                   select d;
        }

        public void Save()
        {
            try
            {
                HandlerFactory.serializer.Serialize<DishesHandler>(this);
            }
            catch { }
        }


    }
}